package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.UserBean;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements IUserService {

    @Autowired
    private IUserMapper mapper;

    @Override
    public UserBean login(String userName, String pwd) {
//        QueryWrapper<UserBean> qw= new QueryWrapper<>();
//        qw.eq("u_name",userName);
//        qw.eq("u_pwd",pwd);
//        UserBean user= mapper.selectOne(qw);
        return mapper.login(userName, pwd);
    }

    @Override
    public boolean addUser(UserBean user) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_name",user.getName());

        UserBean userBean = mapper.selectOne(qw);
        if (userBean != null){
            return false;
        }

        mapper.insert(user);
        return true;

//        if (mapper.check(user.getName()) == null){
//            mapper.addUser(user);
//            return true;
//        }
//        else {
//            return false;
//        }
    }

    @Override
    public boolean checkName(String userName) {
        if (mapper.check(userName) == null){
            return true;
        }else {
            return false;
        }

    }

    @Override
    public List<UserBean> findAll() {
        return mapper.findAll();
    }

    @Override
    public boolean delUser(Integer id, UserBean loginUser) {
        if (id == loginUser.getId()){
            return false;
        }
        mapper.deleteById(id);
        return true;



//
//        if (id == loginUser.getId()){
//            return false;
//        }
//        else {
//            mapper.delUser(id);
//            return true;
//        }
    }

    @Override
    public UserBean findById(Integer id) {
        return mapper.findById(id);
    }

    @Override
    public void updateUserLevel(Integer id, String level) {
        mapper.updateUserLevel(id, level);
    }

    @Override
    public void updateUserPwd(Integer id, String pwd) {
        mapper.updateUserPwd(id, pwd);
    }
}
